// @ts-nocheck
import { store } from "@/store";
export const useParamStore = defineStore("params", () => {
  const data = ref({});

  const hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);

  const setParams = (value, key) => {
    data.value[key] = value;
  };

  const getParams = (key) => {
    return hasOwn(data.value, key) ? data.value[key] : null;
  };

  const compact = (array: any[]): any[] => {
    return array.filter((item) => item);
  };

  const removeFalsyValues = (
    object: Object,
    isDeep: Boolean = false,
    includeArr: Boolean = false
  ) => {
    if (!isObject(object)) return object;

    const newObject = {};
    for (const key in object) {
      if (hasOwn(object, key)) {
        const item = object[key];
        if (!item && item !== 0) continue;

        if (isArray(item)) {
          if (includeArr) {
            newObject[key] = compact(item);
          } else {
            newObject[key] = item;
          }
          continue;
        }
        newObject[key] = isObject(item) && isDeep ? removeFalsyValues(item, isDeep) : item;
      }
    }
    return newObject;
  };

  return {
    setParams,
    getParams,
    removeFalsyValues,
    data,
  };
});

export function useParamStoreHook() {
  return useParamStore(store);
}
